package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.AffichageManager;
import affichage.AffichageDetailCommande;
import beans.Commande;
import beans.User;

/**
 * Servlet implementation class DetailCommandeServlet
 */
public class DetailCommandeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DetailCommandeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getSession().getAttribute("user") != null) {
			Commande cmd = null;
			User u = (User) request.getSession().getAttribute("user");
			for (Commande c : u.getCommandes()) {
				if (c.getId() == Integer.parseInt(request.getParameter("id"))) {
					cmd = c;
				}
				if (cmd != null) {
					AffichageDetailCommande adc = AffichageManager
							.buildAffichageDetailCommande((User) request
									.getSession().getAttribute("user"),
									(Commande) request.getSession()
											.getAttribute("panier"), cmd);
					request.setAttribute("affichage", adc);
					request.getRequestDispatcher("/detailCommande.jsp")
							.forward(request, response);
				} else {
					request.getRequestDispatcher("/home").forward(request,
							response);
				}
			}
		} else {
			request.getRequestDispatcher("/home").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
